package com.itheima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.article.mapper.AuthorMapper;
import com.itheima.article.service.AuthorService;
import com.itheima.model.article.pojos.ApAuthor;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements AuthorService {

    @Override
    public ResponseResult findByUserId(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<ApAuthor> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApAuthor::getId,id);
        List<ApAuthor> list = list(lambdaQueryWrapper);
        if (list!=null&&list.size()>0){
            return ResponseResult.okResult(list.get(0));
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


    }

    @Override
    public ResponseResult insert(ApAuthor apAuthor) {
        if (apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        apAuthor.setCreatedTime(new Date());
        save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
